package com.bootx.service.impl;

import com.bootx.entity.LotteryRecord;
import com.bootx.entity.LotteryRecordNum;
import com.bootx.service.LotteryRecordNumService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LotteryRecordNumServiceImpl extends BaseServiceImpl<LotteryRecordNum,Long> implements LotteryRecordNumService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public Boolean isExist(LotteryRecordNum lotteryRecordNum){
        Integer i = jdbcTemplate.queryForObject("select count(id) from lotteryrecordnum where lotteryCode=? and sn=? and num=?", Integer.class, lotteryRecordNum.getLotteryCode(), lotteryRecordNum.getSn(), lotteryRecordNum.getNum());
        return i!=null&&i>0;
    }


    @Override
    public List<Map<String,Object>> create(LotteryRecord lotteryRecord) {
        List<Map<String,Object>> list = new ArrayList<>();
        if(!lotteryRecord.isNew()){
            String drawCode = lotteryRecord.getDrawCode();
            if(StringUtils.isNotEmpty(drawCode)){
                String[] split = drawCode.split(",");
                for (int i = 0; i < split.length; i++) {
                    Map<String,Object> map = new HashMap<>();
                    LotteryRecordNum lotteryRecordNum = new LotteryRecordNum();
                    lotteryRecordNum.setOrder(i+1);
                    lotteryRecordNum.setLotteryCode(lotteryRecord.getLotteryCode());
                    lotteryRecordNum.setSn(lotteryRecord.getSn());
                    lotteryRecordNum.setNum(Integer.valueOf(split[i]));
                    lotteryRecordNum.setLotteryRecordOrder(lotteryRecord.getOrder());
                    lotteryRecordNum.setDate(lotteryRecord.getDate());
                    map.put("index",lotteryRecordNum.getOrder());
                    map.put("amount",(lotteryRecordNum.getNum()+0.0)/100);
                    list.add(map);
                    if(!isExist(lotteryRecordNum)){
                        super.save(lotteryRecordNum);
                    }
                }
            }
        }
        return list;
    }
}
